/******************************************************************************
 * FileName:    gwin_dev_info_pkt.c
 * Desc:        设备信息数据包
 *                  暂时用在厂测广播
 *
 * Author:      Lin
 * Notes:
 *
 ******************************************************************************/

/*-------------------------- Includes ---------------------------------------*/
#include "gwin_dev_info_pkt.h"
#include "gwin_misc_api.h"


/*-------------------------- Macro Definitions ------------------------------*/
#ifndef GWIN_PROTOCOL_TYPE
#define GWIN_PROTOCOL_TYPE                              GWIN_PROT_TYPE_COMM_B05P
#endif

#ifndef GWIN_PRODUCT_PID
#define GWIN_PRODUCT_PID                                12345           // 产品短PID
#endif

#ifndef GWIN_PROJECT_INDEX
#define GWIN_PROJECT_INDEX                              0x567890        // 产品线编号
#endif

#ifndef GWIN_SDK_MAJOR
#define GWIN_SDK_MAJOR                                  0x0A            // SDK主版本号
#endif

#ifndef GWIN_SDK_MINOR
#define GWIN_SDK_MINOR                                  0x0B            // SDK子版本号
#endif

#ifndef GWIN_SDK_REVISION
#define GWIN_SDK_REVISION                               0x0C            // SDK子版本号
#endif

#ifndef GWIN_PRODUCT_APP_DEVELOPER_VERSION
#define GWIN_PRODUCT_APP_DEVELOPER_VERSION              0001            // SDK子版本号
#endif

#ifndef GWIN_PRODUCT_APP_SUB_VERSION
#define GWIN_PRODUCT_APP_SUB_VERSION                    0000
#endif

#ifndef GWIN_REMOTE_PROC_VERSION
#define GWIN_REMOTE_PROC_VERSION                        GWIN_REMOTE_PROT_TYPE_V3
#endif



#ifndef _TO_STR
#define __TO_STR(a)                                     #a
#define _TO_STR(a)                                      __TO_STR(a)
#endif

#ifndef HL_SWAP_U16
#define HL_SWAP_U16(x)                                  ((((uint16_t)x << 8) & 0xFF00) | ((uint16_t)x >> 8))
#endif
#ifndef HL_SWAP_U32
#define HL_SWAP_U32(x)                                  ((((uint32_t)x << 24) & 0xFF000000) | (((uint32_t)x << 8)&0x00FF0000) | (((uint32_t)x >> 8) & 0x0000FF00) | (((uint32_t)x >> 24) & 0x000000FF))
#endif



/*-------------------------- Type Declarations ------------------------------*/

/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/

/*-------------------------- Functions Declarations -------------------------*/

/*-------------------------- Functions Definitions --------------------------*/
/**
 * @brief 数据校验
 */
static uint8_t gwin_main_dev_info_pkt_data_check(uint8_t *data, uint16_t len)
{
    uint8_t sum = 0;

    for (uint16_t i = 0; i < len; i++)
    {
        sum += data[i];
    }

    return sum;
}


/**
 * @brief 设备信息数据包
 *          串口应答包
 */
void gwin_main_dev_info_pkt_comm_rsp(uint8_t *data, uint8_t len,    // 数据包
                                        uint8_t *mac_addr,          // mac地址
                                        uint8_t ble_adv_scan)       // 连接状态 0：不可发现；1：可发现
{
    if (len < sizeof(dev_info_pkt_comm_rsp_t)) return;

    dev_info_pkt_comm_rsp_t *dev_info_pkt = (dev_info_pkt_comm_rsp_t *)data;
    memset(dev_info_pkt, 0x00, sizeof(dev_info_pkt_comm_rsp_t));
    dev_info_pkt->head.ble_adv_len = GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_comm_rsp_t));
    dev_info_pkt->head.ble_adv_type = GWIN_GAP_ADTYPE_MANUFACTURER_SPECIFIC;
    dev_info_pkt->head.company_id = GWIN_COMPANY_ID;
    dev_info_pkt->head.prot_type = GWIN_PROTOCOL_TYPE;
    dev_info_pkt->head.pid = HL_SWAP_U16(GWIN_PRODUCT_PID);
    dev_info_pkt->head.fw_version.project_index.product_line = GWIN_PRODUCT_LINE_MAIN(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.product_line_sub = GWIN_PRODUCT_LINE_SUB(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.serial_number = GWIN_SERIAL_NUMBER(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.sdk_version.major = GWIN_SDK_MAJOR;
    dev_info_pkt->head.fw_version.sdk_version.minor = GWIN_SDK_MINOR;
    dev_info_pkt->head.fw_version.sdk_version.revision = GWIN_SDK_REVISION;
    dev_info_pkt->head.fw_version.app_version = HL_SWAP_U16(atoi(_TO_STR(GWIN_PRODUCT_APP_DEVELOPER_VERSION)));;
    memcpy(dev_info_pkt->head.mac_addr, mac_addr, sizeof(dev_info_pkt->head.mac_addr));

    dev_info_pkt->flag.ble_adv_scan = ble_adv_scan;
    dev_info_pkt->check_sum = gwin_main_dev_info_pkt_data_check((uint8_t *)&dev_info_pkt->head.company_id, GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_comm_rsp_t)) - 2);   // 和校验从 厂商ID 开始
}

/**
 * @brief 设备信息数据包
 *          串口广播包
 */
void gwin_main_dev_info_pkt_comm_adv(uint8_t *data, uint8_t len,    // 数据包
                                        uint8_t *mac_addr,          // mac地址
                                        uint16_t pid)               // 产品短PID
{
    if (len < sizeof(dev_info_pkt_comm_adv_t)) return;

    dev_info_pkt_comm_adv_t *dev_info_pkt = (dev_info_pkt_comm_adv_t *)data;
    memset(dev_info_pkt, 0x00, sizeof(dev_info_pkt_comm_adv_t));
    dev_info_pkt->head.ble_adv_len = GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_comm_adv_t));
    dev_info_pkt->head.ble_adv_type = GWIN_GAP_ADTYPE_MANUFACTURER_SPECIFIC;
    dev_info_pkt->head.company_id = GWIN_COMPANY_ID;
    dev_info_pkt->head.prot_type = GWIN_PROTOCOL_TYPE;
    dev_info_pkt->head.pid = HL_SWAP_U16(pid);
    dev_info_pkt->head.fw_version.project_index.product_line = GWIN_PRODUCT_LINE_MAIN(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.product_line_sub = GWIN_PRODUCT_LINE_SUB(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.serial_number = GWIN_SERIAL_NUMBER(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.sdk_version.major = GWIN_SDK_MAJOR;
    dev_info_pkt->head.fw_version.sdk_version.minor = GWIN_SDK_MINOR;
    dev_info_pkt->head.fw_version.sdk_version.revision = GWIN_SDK_REVISION;
    dev_info_pkt->head.fw_version.app_version = HL_SWAP_U16(atoi(_TO_STR(GWIN_PRODUCT_APP_DEVELOPER_VERSION)));;
    memcpy(dev_info_pkt->head.mac_addr, mac_addr, sizeof(dev_info_pkt->head.mac_addr));

    dev_info_pkt->sub_version = atoi(_TO_STR(GWIN_PRODUCT_APP_SUB_VERSION));
    dev_info_pkt->check_sum = gwin_main_dev_info_pkt_data_check((uint8_t *)&dev_info_pkt->head.company_id, GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_comm_adv_t)) - 2);   // 和校验从 厂商ID 开始
}

/**
 * @brief 设备信息数据包
 *          米家照明方案
 */
void gwin_main_dev_info_pkt_light(uint8_t *data, uint8_t len,       // 数据包
                                    uint8_t *mac_addr,              // mac地址
                                    uint8_t ac_is_exist,            // AC
                                    uint8_t fill_light_is_exist,    // 辅光
                                    uint8_t pwm_cal_model,          // 驱动方式
                                    uint16_t pwm_min,               // 最低亮度
                                    uint16_t pwm_fre_cold,          // 灯光PWM频率 冷光
                                    uint16_t pwm_fre_warm)          // 灯光PWM频率 暖光
{
    if (len < sizeof(dev_info_pkt_light_t)) return;

    dev_info_pkt_light_t *dev_info_pkt = (dev_info_pkt_light_t *)data;
    memset(dev_info_pkt, 0x00, sizeof(dev_info_pkt_light_t));
    dev_info_pkt->head.ble_adv_len = GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_light_t));
    dev_info_pkt->head.ble_adv_type = GWIN_GAP_ADTYPE_MANUFACTURER_SPECIFIC;
    dev_info_pkt->head.company_id = GWIN_COMPANY_ID;
    dev_info_pkt->head.prot_type = GWIN_PROTOCOL_TYPE;
    dev_info_pkt->head.pid = HL_SWAP_U16(GWIN_PRODUCT_PID);
    dev_info_pkt->head.fw_version.project_index.product_line = GWIN_PRODUCT_LINE_MAIN(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.product_line_sub = GWIN_PRODUCT_LINE_SUB(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.serial_number = GWIN_SERIAL_NUMBER(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.sdk_version.major = GWIN_SDK_MAJOR;
    dev_info_pkt->head.fw_version.sdk_version.minor = GWIN_SDK_MINOR;
    dev_info_pkt->head.fw_version.sdk_version.revision = GWIN_SDK_REVISION;
    dev_info_pkt->head.fw_version.app_version = HL_SWAP_U16(atoi(_TO_STR(GWIN_PRODUCT_APP_DEVELOPER_VERSION)));;
    memcpy(dev_info_pkt->head.mac_addr, mac_addr, sizeof(dev_info_pkt->head.mac_addr));

    dev_info_pkt->ac_is_exist = ac_is_exist;
    dev_info_pkt->fill_light_is_exist = fill_light_is_exist;
    dev_info_pkt->pwm_cal_model = pwm_cal_model;
    dev_info_pkt->pwm_min = pwm_min;
    dev_info_pkt->pwm_fre_cold = pwm_fre_cold / 100;
    dev_info_pkt->pwm_fre_warm = pwm_fre_warm / 100;
    dev_info_pkt->remote_proc_version = GWIN_REMOTE_PROC_VERSION;
    dev_info_pkt->sub_version = atoi(_TO_STR(GWIN_PRODUCT_APP_SUB_VERSION));
    dev_info_pkt->check_sum = gwin_main_dev_info_pkt_data_check((uint8_t *)&dev_info_pkt->head.company_id, GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_light_t)) - 2);   // 和校验从 厂商ID 开始
}

/**
 * @brief 设备信息数据包
 *          米家电工方案
 */
void gwin_main_dev_info_pkt_elec(uint8_t *data, uint8_t len,        // 数据包
                                    uint8_t *mac_addr,              // 当前供电方式
                                    uint8_t elec_type,              // 按键数 1-8键
                                    uint8_t key_cnt)                // 有无背光
{
    if (len < sizeof(dev_info_pkt_elec_t)) return;

    dev_info_pkt_elec_t *dev_info_pkt = (dev_info_pkt_elec_t *)data;
    memset(dev_info_pkt, 0x00, sizeof(dev_info_pkt_elec_t));
    dev_info_pkt->head.ble_adv_len = GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_elec_t));
    dev_info_pkt->head.ble_adv_type = GWIN_GAP_ADTYPE_MANUFACTURER_SPECIFIC;
    dev_info_pkt->head.company_id = GWIN_COMPANY_ID;
    dev_info_pkt->head.prot_type = GWIN_PROTOCOL_TYPE;
    dev_info_pkt->head.pid = HL_SWAP_U16(GWIN_PRODUCT_PID);
    dev_info_pkt->head.fw_version.project_index.product_line = GWIN_PRODUCT_LINE_MAIN(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.product_line_sub = GWIN_PRODUCT_LINE_SUB(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.project_index.serial_number = GWIN_SERIAL_NUMBER(GWIN_PROJECT_INDEX);
    dev_info_pkt->head.fw_version.sdk_version.major = GWIN_SDK_MAJOR;
    dev_info_pkt->head.fw_version.sdk_version.minor = GWIN_SDK_MINOR;
    dev_info_pkt->head.fw_version.sdk_version.revision = GWIN_SDK_REVISION;
    dev_info_pkt->head.fw_version.app_version = HL_SWAP_U16(atoi(_TO_STR(GWIN_PRODUCT_APP_DEVELOPER_VERSION)));;
    memcpy(dev_info_pkt->head.mac_addr, mac_addr, sizeof(dev_info_pkt->head.mac_addr));

    dev_info_pkt->elec_type = elec_type;
    dev_info_pkt->key_cnt = key_cnt;
    dev_info_pkt->remote_proc_version = GWIN_REMOTE_PROC_VERSION;
    dev_info_pkt->sub_version = atoi(_TO_STR(GWIN_PRODUCT_APP_SUB_VERSION));
    dev_info_pkt->check_sum = gwin_main_dev_info_pkt_data_check((uint8_t *)&dev_info_pkt->head.company_id, GWIN_GAP_ADV_PAYLOAD_LEN(sizeof(dev_info_pkt_elec_t)) - 2);   // 和校验从 厂商ID 开始
}


/*-------------------------- END OF FILE ------------------------------------*/

